<template>
  <div ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts" setup>
  import { onMounted, ref, Ref } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { StdResponse } from '/@/api/model/baseModel';
  import { getAlarmLogsCount } from '/@/api/monitor/rule';
  import { GetAlarmLogRsp } from '/@/api/monitor/entity/entity';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { basicProps } from '/@/views/object/detail/visit/props';
  import { useRouter } from 'vue-router';

  defineProps({
    ...basicProps,
  });

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  const { createMessage } = useMessage();
  const { currentRoute } = useRouter();

  const loadData = async () => {
    const rsp: StdResponse = await getAlarmLogsCount({
      length: 7 * 24,
      object_id: Number(currentRoute.value.params.id),
    });
    if (!rsp) {
      createMessage.error('获取数据失败');
    }
    const data: GetAlarmLogRsp = rsp;

    setOptions({
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          lineStyle: {
            width: 1,
            color: '#019680',
          },
        },
      },
      grid: { left: '1%', right: '1%', top: '2  %', bottom: 0, containLabel: true },
      xAxis: {
        type: 'category',
        data: data.axis_x,
      },
      yAxis: {
        type: 'value',
        max: data.count,
        splitNumber: 4,
      },
      series: [
        {
          data: data.axis_y,
          type: 'bar',
          barMaxWidth: 80,
        },
      ],
    });
  };

  onMounted(() => {
    loadData();
  });
</script>
